//
//  Tokenizer.cpp
//  Solver
//
//  Created by Nicolas Winkler on 12.07.13.
//  Copyright (c) 2013 Nicolas Winkler. All rights reserved.
//

#include "Tokenizer.h"

using solver::Tokenizer;


Tokenizer::Tokenizer(const std::string& input)
{
    this->input = input;
}


Tokenizer::~Tokenizer(void)
{
}


void Tokenizer::run(void)
{
    Token token;
    token.token = "";
    tokens.clear();
    for (unsigned int i = 0; i < input.length(); i++) {
        char chr = input[i];
        CharType type = charType(chr);
        if (type == NORMAL) {
            token.token += chr;
            token.type = Token::TEXT;
        }
        else if (type == NUMBER) {
            token.token += chr;
            token.type = Token::NUMBER;
        }
        else if (type == SPACE) {
            if (token.token != "")
                tokens.push_back(token);
            token.token = "";
        }
        else if (type == TOKEN) {
            if (token.token != "")
                tokens.push_back(token);
            token.token = chr;
            token.type = Token::SYMBOL;
            tokens.push_back(token);
            token.token = "";
        }
    }
    if (token.token != "")
        tokens.push_back(token);
}


const solver::Token& Tokenizer::getToken(unsigned int id) const
{
    return tokens[id];
}


unsigned long Tokenizer::getNTokens(void) const
{
    return tokens.size();
}


Tokenizer::CharType Tokenizer::charType(char chr)
{
    if ((chr >= 65 && chr <= 90) ||
        (chr >= 97 && chr <= 122)) {
        return NORMAL;
    }
    else if ((chr >= 48 && chr <= 57)) {
        return NUMBER;
    }
    else if (chr == 32 || chr == '\t' || chr == '\n') {
        return SPACE;
    }
    else {
        return TOKEN;
    }
}



